package com.xzz.web.controller;

import com.xzz.enums.GlobalErrorCode;
import com.xzz.exception.GlobalCustomException;
import com.xzz.service.IMessageBucksmsService;
import com.xzz.domain.MessageBucksms;
import com.xzz.query.MessageBucksmsQuery;
import com.xzz.result.JSONResult;
import com.xzz.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import com.xzz.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/messageBucksms")
public class MessageBucksmsController {

    @Autowired
    public IMessageBucksmsService messageBucksmsService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody MessageBucksms messageBucksms){
        //扩展：进行黑名单校验
        MessageBucksms buckByPhone = messageBucksmsService.getBuckByPhone(messageBucksms.getPhone());
        AssertUtil.isNull( buckByPhone , GlobalErrorCode.SERVICE_PARAM_IS_BUCK );

        if(messageBucksms.getId()!=null){
            messageBucksmsService.updateById(messageBucksms);
        }else{
            messageBucksmsService.insert(messageBucksms);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/del/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        messageBucksmsService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(messageBucksmsService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(messageBucksmsService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody MessageBucksmsQuery query){
        Page<MessageBucksms> page = new Page<MessageBucksms>(query.getPage(),query.getRows());
        page = messageBucksmsService.selectPage(page);
        return JSONResult.success(new PageList<MessageBucksms>(page.getTotal(),page.getRecords()));
    }
}
